(function(){
    //文字初始化
    var deviceWidth = document.documentElement.clientWidth;
    if (deviceWidth > 750) deviceWidth = 750;
    document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
})()

$(function(){
    createTang();
    CountDown();
});



var maxtime = 90  //按秒计算，自己调整! 
var Fraction;
var t ;
function CountDown() {
    if (maxtime >= 0) {
        var timer = setInterval(function(){
            minutes = Math.floor(maxtime / 60);
            seconds = Math.floor(maxtime % 60);
            if (seconds<10) {
                seconds =  "0" + seconds 
            }
            msg =  minutes + ":" + seconds;
            $('.count').text(msg);
            maxtime --;
            if (maxtime < 0){
                clearInterval(timer);
                clearInterval(t);
                alert("时间到，游戏结束，恭喜得到"+Fraction+"分");
            }
        },1000);
        
    } 
}
function shanBox(){
    $('body').append('<div class="shan">+10</div>');
    var t2=setTimeout(function(){
        $('body').find('.shan').remove();
    },800)
}


function createTang(){
    Fraction = 0;
    var i = 1 ;
    var sjNumber,sjNumberBox;
    sjNumberBox = [];
    t = setInterval(function(){
        sjNumber = Math.round(Math.random()*10);
        if (sjNumber < 1) {
           sjNumber = Math.round(Math.random()*10)+2;
        }
        sjNumberBox.push(sjNumber);
        //console.log(sjNumberBox);
        var cube = '<div class="ty" id="'+i+'"></div>';
        $('.Buy-box').before(cube);
       
             
        $('body').find('#'+i).css({
            top: sjNumberBox[i-1]+'rem'
        });

        $('body').find('.ty').animate({right:'100%'}, 4000,function(){
            if ($(this).offset().left <= 0) {
                $(this).remove();
            }
        });
        i++;
    }, 600);
    var _left = $('.Buy-box').offset().left,
        _top  = $('.Buy-box').offset().top

    $('body').on('touchstart','.ty',function(event){
        var _x,_y;
        $(this).stop(false,false).animate();
        $(this).on('touchmove',function(event){
             var event = event || window.event;
            $(this).stop(false,false).animate();
             _x = event.changedTouches[0].pageX - 25;
             _y = event.changedTouches[0].pageY - 25;
            $(this).css({
                left: _x + 'px',
                top:  _y + 'px'
            });
            
        });
        $(this).on('touchend',function(){
            //捕获成功
            if(_x > _left&&_y >_top&&_x < (_left+$('.Buy-box').width()-50)){
                $(this).stop(true,true).animate();
                Fraction = Fraction + 10 ;
                shanBox();
                $('.jifen').text(Fraction);

            }else{
                //捕获失败
                if (_y >_top) {
                    $(this).css({
                        left: ''
                    });
                    $(this).animate({top: sjNumber + 'rem'},300);
                }else{
                    $(this).css({
                        left: ''
                    });
                }  
            }
        });
    });


}